<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/1
 * Time: 22:55
 */


/*
 * 支付基类
 *主要开发:https://docs.open.alipay.com/270
 * 未使用开发SDK自助签名规则:https://docs.open.alipay.com/291/106118
 *              自助验证签名：https://docs.open.alipay.com/200/106120
 */
class AlipayBase{

    protected static $appId="2016091000481004";
    protected static $alipayPubKey="-----BEGIN RSA PRIVATE KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6vLNk6F5q3vlsswDNRvUq3KVbZ0UznWYCim3ZPj2XNfVJClAvHuEMpBB+n368nxaJC9bis+sgxZVIkgvAEbSrr7KHIUJ1EhzkDU8C1eVIPpYrX8/u077NlEzDIQbUiw5RWzTz7hybtqHYfRWBeZQN7RXWa13PFSx0QcDqV7uBAmOnXKShYx+vqlEIYmg47EtZuXgFitjeiud9rVWbrL/Qvu63ULODAhCBs0RIbmXxsCc5cpuwx/LMWuZAC8/qUH+7gaDGpr9btFaMa7+Qrct+rR9n+shsRbNo/jcVH/X/xeGsT/7Rqih3CFDaSWkJHBJrcXuyYDEgxxhQkI6I4ZsqwIDAQAB-----END RSA PRIVATE KEY-----";
    protected static $priKey="MIIEowIBAAKCAQEAprj6F8yS9W3BcpbmAM3tWewaKkiur/xOftTT8ntNt68HUeNw+TT9GaBs/RYAUl/G8ZGGN1kIRlir14dIpmLvrsRlkLCjl6youqEYGC2sUNGYeCba81T1o1ZgmQpTDZq2TL8gScKJlGRjdDtJAevVB9YXBJkKxpTRFB8iHF4J7Q6BSA+hnQl72GngZR178szzmFPSwyFA6U4zlxxjOzf8+MsQonaPDWjAlvq2jpqmAm/hREtnsEoxTj1tTuCR41CCAJ3uzkA62xtIygipUr50hbClP0DiTYtk0St8icqc5XPA4D0Y0wtNcB+VEAJtZstxAZ7HlJ5Y09NFV8rL87GXowIDAQABAoIBAEaKg36oVxGy46OpRx3WA3QFmriDUi4x8cBmKlsxYpuyWcWDFxzKF8DNCPA4a+Bul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izaGf721wpyxucvOjO7N0qW77nS65i4KZzAOkWxYMwCGOwvoyXTK5uVgGSqDmPo6A5D";
    protected static $payGateway = "https://openapi.alipaydev.com/gateway.do";
    protected  static $sign_type = "RSA2";
    protected static  $charset = "UTF-8";
    protected  static $returnUrl = "http://www.cc8w.com/alipay/return.php";
    protected  static $notifyUrl = "http://www.cc8w.com/alipay/notify.php";

    public function __construct()
    {

    }

    public function getSignStr($parm)
    {//获取签名字符串
        if(isset($parm['sign']))
        {
            unset($parm['sign']);
        }
//        if(isset($parm['sign_type']))
//        {
//            unset($parm['sign_type']);
//        }
        //1.先进行字典序排序
        ksort($parm);
        //2.转换成url参数形式
        $parm = $this->getUrlStr($parm,false);
        return $parm;

    }
    public function checkSignStr()
    {//验证签名字符串

    }
    public  function rsa2Sign($data)
    {//RSA2加密


        $str        = trim(self::$priKey);
        $str        = chunk_split($str, 64, PHP_EOL);//在每一个64字符后加一个\n
        $key = "-----BEGIN RSA PRIVATE KEY-----".PHP_EOL.$str."-----END RSA PRIVATE KEY-----".PHP_EOL;
        $signature = '';
        if (openssl_sign($data, $signature, openssl_pkey_get_private($key), OPENSSL_ALGO_SHA256)) {
            $res = base64_encode($signature);
            return $res;
        }


    }
    public  function checkRsa2Sign()
    {//RSA2解密验证签名

    }

    public function getUrlStr($data,$type=true)
    {//拼接成url字符串
        if($type)
        {//浏览器传递,默认正确:把中文字符已经encode了
            return http_build_query($data);
        }
        else
        {//参数传递,我们不希望把 中文字符encode，所以还原一下
            return urldecode(http_build_query($data));
        }

    }




}